﻿using System.Collections.ObjectModel;
using MSDNWebcast.Service;
using MSDNWebcast.Models;
using System.Collections.Generic;
using System;

namespace MSDNWebcast.ViewModels
{
    /// <summary>
    /// 下载列表视图
    /// </summary>
    public class DownloadsViewModel : WorkspaceViewModel
    {
        #region Fields

        private ObservableCollection<CourseFileViewModel> m_DownloadList;

        #endregion

        #region Constructors

        public DownloadsViewModel()
        {
            PageInfo = "正在下载";
        }

        #endregion

        #region Properties

        public override string DisplayName
        {
            get { return "下载管理"; }
        }

        /// <summary>
        /// 下载列表
        /// </summary>
        public ObservableCollection<CourseFileViewModel> DownloadList
        {
            get
            {
                if (m_DownloadList == null)
                {
                    m_DownloadList = new ObservableCollection<CourseFileViewModel>();
                    m_DownloadList.CollectionChanged += m_DownloadList_CollectionChanged;
                    var downloads = ServiceFactory.Instance.GetService<DownloadService>().DownloadList;
                    foreach (var downloadItem in downloads)
                    {
                        var courseFile = new CourseFileViewModel(downloadItem.Key);
                        m_DownloadList.Add(courseFile);
                    }
                    ServiceFactory.Instance.GetService<DownloadService>().CourseFileBeginDownloaded += DownloadsViewModel_CourseFileBeginDownloaded;
                }
                return m_DownloadList;
            }
        }

        /// <summary>
        /// 显示提示信息
        /// </summary>
        public bool ShowInfomation
        {
            get { return DownloadList.Count <= 0; }
        }

        /// <summary>
        /// 显示下载列表
        /// </summary>
        public bool ShowDownloadList
        {
            get { return DownloadList.Count > 0; }
        }

        #endregion

        #region Methods


        /// <summary>
        /// 添加视图显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void DownloadsViewModel_CourseFileBeginDownloaded(object sender, CourseFileBeginDownloadedEventArgs e)
        {
            m_DownloadList.Add(new CourseFileViewModel(e.CourseFile));
        }

        void m_DownloadList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            base.OnPropertyChanged("ShowInfomation");
            base.OnPropertyChanged("ShowDownloadList");
        }

        #endregion
    }
}
